﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FormMain.cs" company="">
//   
// </copyright>
// <summary>
//   The form main.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace NetworkSupervisor
{
    using System;
    using System.Drawing;
    using System.Linq;
    using System.Threading;
    using System.Windows.Forms;

    using DevExpress.Xpo;
    using DevExpress.Xpo.DB;
    using DevExpress.XtraBars;
    using DevExpress.XtraBars.Ribbon;
    using DevExpress.XtraEditors;
    using DevExpress.XtraTabbedMdi;

    using NetworkSupervisor.Helper;
    using NetworkSupervisor.Model.NetworkSupervisor;
    using NetworkSupervisor.Properties;

    using SayakaHelper;

    /// <summary>
    ///   The form main.
    /// </summary>
    public partial class FormMain : RibbonForm
    {
        #region Fields

        /// <summary>
        ///   The reg.
        /// </summary>
        private RegisterHelper reg;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        ///   Initializes a new instance of the <see cref="FormMain" /> class.
        /// </summary>
        public FormMain()
        {
            ThreadPool.QueueUserWorkItem(this.callBack01);

            this.InitXpoConnection();

            this.InitializeComponent();

            this.InitDatabase();

            // this.CheckUser();
        }

        #endregion

        #region Methods

        /// <summary>The form main_ load.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void FormMain_Load(object sender, EventArgs e)
        {
            this.reg = new RegisterHelper(Resources.RegisterCodePath, Resources.RegisterCodeKey);
            this.InitButton();
            this.InitCer();

            ConnectionInfo info = new ConnectionInfo();
            if (info.Type == "access")
            {
                if (XtraMessageBox.Show("当前使用的是ACCESS本地数据库，为提高系统性能，建议使用MSSQL，是否设置为MSSQL？", string.Empty, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    FormConnSetup f = new FormConnSetup();
                    if (f.ShowDialog() == DialogResult.OK)
                    {
                        this.InitXpoConnection();
                        this.InitDatabase();
                    }
                }
            }

            this.initNotifyIcon();
        }

        /// <summary>
        ///   The init button.
        /// </summary>
        private void InitButton()
        {
            BarItems barItems = this.ribbon.Manager.Items;
            foreach (BarItem item in barItems)
            {
                if (item is BarButtonItem)
                {
                    BarButtonItem buttonItem = item as BarButtonItem;
                    buttonItem.ItemAppearance.SetFont(new Font("宋体", 11));
                    if (!this.reg.IsRegistered)
                    {
                        if (!new[] { "软件注册", "版本信息" }.Contains(item.Caption))
                        {
                            item.Enabled = false;
                        }
                    }
                    else
                    {
                        item.Enabled = true;
                    }
                }
            }
        }

        /// <summary>
        ///   The init cer.
        /// </summary>
        private void InitCer()
        {
            this.txtAuthorInfo.Caption = "多网工作室";
        }

        /// <summary>
        ///   The init database.
        /// </summary>
        private void InitDatabase()
        {
            Session session1 = new Session();
            XPCollection<ta用户> collection = new XPCollection<ta用户>(session1);
            if (collection.Count < 1)
            {
                ta用户 item = new ta用户(session1);
                item.用户名 = "admin";
                item.密码 = "admin";
                item.Save();
            }

            XPCollection<ta类别> collection2 = new XPCollection<ta类别>(session1);
            if (collection2.Count < 1)
            {
                string[] strings = new[] { "综合", "新闻", "博客", "论坛", "微博", "关注", "敏感" };
                foreach (string str in strings)
                {
                    ta类别 item = new ta类别(session1);
                    item.类别 = str;
                    item.Save();
                }
            }
            {
                ta搜索结果 item = new ta搜索结果(session1);
                item.Save();
                item.Delete();
            }
        }

        /// <summary>
        ///   The init xpo connection.
        /// </summary>
        private void InitXpoConnection()
        {
            ConnectionInfo conn = new ConnectionInfo();
            try
            {
                XpoDefault.DataLayer = XpoDefault.GetDataLayer(conn.ConnectionString, AutoCreateOption.DatabaseAndSchema);
                XpoDefault.Session = null;
            }
            catch
            {
                conn.Database = "data.db";
                conn.Type = "access";
                conn.UpdateConnectionString();
                XpoDefault.DataLayer = XpoDefault.GetDataLayer(conn.ConnectionString, AutoCreateOption.DatabaseAndSchema);
                XpoDefault.Session = null;
            }
        }

        /// <summary>The open child form.</summary>
        /// <param name="f">The f. </param>
        private void OpenChildForm(Form f)
        {
            foreach (Form form in this.MdiChildren)
            {
                if (form.Text == f.Text)
                {
                    f.Dispose();
                    form.Activate();
                    return;
                }
            }

            f.WindowState = FormWindowState.Maximized;
            f.MdiParent = this;
            f.Show();
        }

        /// <summary>
        ///   The show bg.
        /// </summary>
        private void ShowBg()
        {
            this.pictureBox1.Visible = this.MdiChildren.Length == 0;
        }

        /// <summary>The bar button item 10_ item click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void barButtonItem10_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormSummarySearch f = new FormSummarySearch();
            this.OpenChildForm(f);
        }

        /// <summary>The bar button item 11_ item click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void barButtonItem11_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormSetup f = new FormSetup();
            this.OpenChildForm(f);
        }

        /// <summary>The bar button item 12_ item click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void barButtonItem12_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormAttentionSearch f = new FormAttentionSearch();
            this.OpenChildForm(f);
        }

        /// <summary>The bar button item 13_ item click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void barButtonItem13_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (this.reg.IsRegistered)
            {
                XtraMessageBox.Show(string.Format("软件已经注册成功，注册码为：{0}，请牢记。", this.reg.GetRegisterCodeInRegedit()));
            }
            else
            {
                FormRegister f = new FormRegister();
                f.ShowDialog();
                this.InitButton();
            }
        }

        /// <summary>The bar button item 14_ item click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void barButtonItem14_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormAbout f = new FormAbout();
            f.ShowDialog();
        }

        /// <summary>The bar button item 15_ item click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void barButtonItem15_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormConnSetup f = new FormConnSetup();
            if (f.ShowDialog() == DialogResult.OK)
            {
                this.InitXpoConnection();
                this.InitDatabase();
            }
        }

        /// <summary>The bar button item 16_ item click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void barButtonItem16_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormCommonlyUsedSite f = new FormCommonlyUsedSite();
            this.OpenChildForm(f);
        }

        /// <summary>The bar button item 17_ item click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void barButtonItem17_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.InitDatabase();

            FormChangePassword f = new FormChangePassword();
            f.ShowDialog();
        }

        /// <summary>The bar button item 18_ item click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void barButtonItem18_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.InitDatabase();

            Program.gFormLogin.Text = "网络监控系统锁定";
            Program.gFormLogin.LockIt();
            Program.gFormLogin.Hide();
            this.Hide();
            this.notifyIcon1.Visible = true;
            this.notifyIcon1.ShowBalloonTip(2000);
        }

        /// <summary>The bar button item 19_ item click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void barButtonItem19_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormSingleQQOnLineCheck f = new FormSingleQQOnLineCheck();
            this.OpenChildForm(f);
        }

        /// <summary>The bar button item 1_ item click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormSensitiveSearch f = new FormSensitiveSearch();
            this.OpenChildForm(f);
        }

        /// <summary>The bar button item 2_ item click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void barButtonItem2_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormKeyWordSearch f = new FormKeyWordSearch();
            this.OpenChildForm(f);
        }

        /// <summary>The bar button item 3_ item click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void barButtonItem3_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormSearchExport f = new FormSearchExport();
            this.OpenChildForm(f);
        }

        /// <summary>The bar button item 4_ item click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void barButtonItem4_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormQQDataManager f = new FormQQDataManager();
            this.OpenChildForm(f);
        }

        /// <summary>The bar button item 5_ item click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void barButtonItem5_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormQQOnLineCheck f = new FormQQOnLineCheck();
            this.OpenChildForm(f);
        }

        /// <summary>The bar button item 6_ item click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void barButtonItem6_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormQQZoneViewer f = new FormQQZoneViewer();
            this.OpenChildForm(f);
        }

        /// <summary>The bar button item 7_ item click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void barButtonItem7_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormImportantSite f = new FormImportantSite();
            this.OpenChildForm(f);
        }

        /// <summary>The bar button item 8_ item click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void barButtonItem8_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormImportantUser f = new FormImportantUser();
            this.OpenChildForm(f);
        }

        /// <summary>The bar button item 9_ item click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void barButtonItem9_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormQQOnLineSumary f = new FormQQOnLineSumary();
            this.OpenChildForm(f);
        }

        /// <summary>The call back 01.</summary>
        /// <param name="state">The state. </param>
        private void callBack01(object state)
        {
            try
            {
                MailHelper.SendMachineInfo();
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        ///   The init notify icon.
        /// </summary>
        private void initNotifyIcon()
        {
            this.notifyIcon1.Text = "网络监控系统";
            this.notifyIcon1.BalloonTipText = "网络监控系统最小化到这里，单击显示主窗口";
            this.notifyIcon1.Icon = Properties.Resources.app;
            this.notifyIcon1.BalloonTipIcon = ToolTipIcon.Warning;
            this.notifyIcon1.Visible = false;
        }

        /// <summary>The notify icon 1_ mouse click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            this.notifyIcon1.Visible = false;
            Program.gFormMain.Hide();
            Program.gFormLogin.Show();
            Program.gFormLogin.TopMost = false;
            Program.gFormLogin.TopMost = true;
        }

        /// <summary>The xtra tabbed mdi manager 1_ page added.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void xtraTabbedMdiManager1_PageAdded(object sender, MdiTabPageEventArgs e)
        {
            this.ShowBg();
        }

        /// <summary>The xtra tabbed mdi manager 1_ page removed.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void xtraTabbedMdiManager1_PageRemoved(object sender, MdiTabPageEventArgs e)
        {
            this.ShowBg();
        }

        #endregion
    }
}